const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');

/**
 * Get MinMax voice list
 * @param {Object} req - Express request object
 * @param {Object} res - Express response object
 */
exports.getVoiceList = async (req, res) => {
  try {
    const apiKey = process.env.MINMAX_API_KEY;
    const url = 'https://api.minimax.chat/v1/get_voice';
    
    // Headers
    const headers = {
      'authority': 'api.minimax.chat',
      'Authorization': `Bearer ${apiKey}`
    };

    // Request body
    const data = {
      voice_type: 'all'
    };

    // Configure axios request
    let axiosConfig = {
      headers: headers
    };

    // Add proxy if enabled in environment
    if (process.env.USE_PROXY === 'true') {
      const proxyUrl = `${process.env.PROXY_PROTOCOL}://${process.env.PROXY_HOST}:${process.env.PROXY_PORT}`;
      axiosConfig.httpsAgent = new HttpsProxyAgent(proxyUrl);
      console.log('Using proxy:', proxyUrl);
    }

    const response = await axios.post(url, data, axiosConfig);
    
    return res.status(200).json(response.data);
  } catch (error) {
    console.error('Error fetching MinMax voice list:', error.message);
    return res.status(500).json({
      success: false,
      message: 'Failed to fetch voice list',
      error: error.message
    });
  }
};
